/*
 * This file uses an anonymous function to kick off the page's javascript functionality
 *
 */        
var closeHtml = "<img src=\"/css/colorbox/images/close.png\" alt=\"Close\" width=\"27\" height=\"27\">";
var loadingHtml = "<div class=\"loading\"><img src=\"/images/loading.gif\" alt=\"Loading...\"></div>";
$(function(){    
    $('.answer-comment-answer').live("focus", function() {
        $this = $(this);
        $button = $this.parent().siblings('.button-actions').find('button');
        $button.removeClass('hide');
    }).live("blur", function(){
        $this = $(this);
        $button = $this.parent().siblings('.button-actions').find('button');

        // if empty then show up the label
        if(!$.trim($this.val())) {
            $button.addClass('hide');
        }
    });
    
    // handle prompting text
    $promptText = $('.prompt-text');
    $promptText.live("keypress", function() {
        $this = $(this);
        $thisLabel = $this.parent().find(":eq(0)");
        $thisLabel.addClass("label-invisible");
        $thisLabel.removeClass('label-visible');
        $this.addClass('user-text');
    }).live("blur", function(){
        $this = $(this);

        // grab the label of this element
        $thisLabel = $this.parent().find(":eq(0)");

        // if empty then show up the label
        if(!$.trim($this.val())) {
            $thisLabel.addClass("label-visible");
            $thisLabel.removeClass('label-invisible');
            $this.removeClass('user-text');
        }
        else{
            $thisLabel.addClass("label-invisible");
            $thisLabel.removeClass('label-visible');
            $this.addClass('user-text');
        }
    });

    // hide element's error on focus
    $formElement = $('form .prompt-text, .input input, .input textarea, .select-box-small, .select-box, input.checkbox');
    $formElement.live("focus", function() {
        $this = $(this);
        $("#" + $this.attr("id") + "_error").addClass("error-invisible").removeClass("error-visible");
    }).live("click", function() {
        $this = $(this);
        $("#" + $this.attr("id") + "_error").addClass("error-invisible").removeClass("error-visible");
    });

    // cater for the checkbox elements
    $("label.checkbox").live("click", function() {
        $this = $(this);
        $checkBoxError = $("#" + $this.attr("for") + "_error");
        if($checkBoxError != undefined)
        {
            $checkBoxError.addClass("error-invisible").removeClass("error-visible");
        }
    });

    // update the select picker's text with the actual value
    $('.select-box-small, .select-box').each(function(){
        $(this).siblings(".selection-picker").find(":eq(0)").html($(this).find("option:selected").text());                
    });

    // don't show the labels for non-empty input elements
    $('form .prompt-text').each(function(){
        $this = $(this);
        $thisLabel = $this.parent().find(":eq(0)");

        if($.trim($this.val())) {
            $thisLabel.addClass("label-invisible");
            $this.addClass('user-text');
        }
    });
    
    // request login
    $("#register a, #sign-in a").click(function(){            
        requestLogin();
    });

    // hide and show DocPlus' goodies
    $(".benefit h2").click(function(){
        $('.benefit span').hide();
        $(this).siblings().show();
    });

    $('#more-or-less-inner').click(function(){
        $('.view-more').toggle();
        $('.view-less').toggle();
    });

    // handle all the onselect events on all my custom select elements
    $(".select-box-small, .select-box").live("change", function(){
        $(this).siblings(".selection-picker").find(":eq(0)").html($(this).find("option:selected").text());
                
        if($(".my_appointment_reason_id") != undefined){
            $(this).siblings(".my_appointment_reason_id").find(":eq(0)").html($(this).find("option:selected").text());                    
        }
    });

    $(".select-box-small, .select-box").live("focus", function(){
        // to get the images from the designer
        });
        
    // handle each slot click
    $(".doctor-action-open").click(function(){
        // check to see if the appointment reason field exists
        var $appointmentReasonId = $("#book_now_appointment_reason_id").val();
        if($appointmentReasonId == undefined || $appointmentReasonId == ""){
            $appointmentReasonId = 0;
        }
        
        window.location = "/appointment-book-now-step-one/" + $(this).attr("id") + "/" + $appointmentReasonId;
    });
    
    // submit question answer comments    
    $('.submit-answer-comment').live("click", function() {
        $this = $(this);
        $answerId = $this.attr('answerid');         
        
        $('#answer-comment-form-' + $answerId).hide();
        $('#answer-comment-form-holder-' + $answerId).append(loadingHtml);
            
        $('#answer-comment-form-' + $answerId).ajaxSubmit(function(data){ 
            if(hasForm(data))
            {         
                $('#answer-comment-form-holder-' + $answerId).html(data);
            }
            else{ 
                if(data == 'signin'){
                    requestLogin();
                }
                else{
                    $.get('/answer/comment/show/' + data, {}, function(commentData){   
                        $('#answer-comments-' + $answerId).append(commentData);
                    }, 'html');                    
                    
                    // increment the comments count
                    var commentsCount = $('#comments-count-' +$answerId).html();                    
                    commentsCount = parseInt(commentsCount) + 1;                    
                    $('#comments-count-' + $answerId).html(commentsCount);
                    
                    $('#answer-comment-form-holder-' + $answerId).load('/answer/comment/new/' + $answerId);
                }
            }
        });
        return false;
    });   
    
    // handle thank yous
    $('.answer-stats-item').live('click', function() {
        if($(this).hasClass('answer-unthanked')){
            $(this).removeClass('answer-unthanked');
            $(this).addClass('answer-thanked');                   
                    
            // increment the thanks count
            var thanksCount = $(this).html();                    
            thanksCount = parseInt(thanksCount) + 1;                    
            $(this).html(thanksCount);
        }
        else if($(this).hasClass('answer-thanked')){
            $(this).removeClass('answer-thanked');
            $(this).addClass('answer-unthanked');                   
                    
            // decrement the thanks count
            var thanksCount = $(this).html();                    
            thanksCount = parseInt(thanksCount) - 1;                    
            $(this).html(thanksCount);                
        }
            
        // give thanks
        $.get('/answer/thanks/' + $(this).attr('answerid'), {}, function(){}, 'html');
    });          
    
    // handle follow ups
    $('.connet').live('click', function() {
        $this = $(this);
        if($this.attr('status') == 'open'){
            url = '/connection-follow/' + $this.attr('userid');
        }
        else if($this.attr('status') == 'accepted'){
            url = '/connection-unfollow/' + $this.attr('userid');
        }
        $.get(url, {}, function(data){
            if(data == 'signin'){
                requestLogin();
            }
            else{
                if(data == 'following'){
                    $this.html('Following');
                    $this.removeClass('doctor-follow');
                    $this.addClass('doctor-following');
                    $this.attr('status', 'accepted');
                }
                else{
                    $this.html('Follow');
                    $this.removeClass('doctor-following');
                    $this.addClass('doctor-follow');
                    $this.attr('status', 'open');                    
                }
            }
        }, 'html');
    });       
    
    // handle doctor's network tabs
    $('.network-tabs div').live('click', function() {
        $this = $(this);
        if(!$this.hasClass('active')){
            $this.siblings().removeClass('active');
            $this.addClass('active');
            
            // hide/show tab content
            $('.connections').hide();
            $('#' + $this.attr('tab')).removeClass("hide").show();
        }        
    });
        
    $('#doctor-header li a, #profile-header li a').click(function(){
        $(".doctor-body, .profile-body-left").hide();
        $("#doctor-header li, #profile-header li").removeClass("active");
        $(this).parent().addClass("active")
        $("#" + $(this).attr("tab")).removeClass("hide").show();
    });
        
    $('#member-header-left li a').click(function(){
        $(".member-body-left").hide();
        $("#member-header-left li").removeClass("active");
        $(this).parent().addClass("active")
        $("#" + $(this).attr("tab")).removeClass("hide").show();
    });
        
    $('#member-header-right li a').click(function(){
        $(".member-body-right").hide();
        $("#member-header-right li").removeClass("active");
        $(this).parent().addClass("active")
        $("#" + $(this).attr("tab")).removeClass("hide").show();
    });
});

function doRegister(option){
    window.location = "/" + option + "-register-step-one";
}

function showSignInDropDown(){    
    $("#dropdown").css({
        visibility: "visible"
    });
            
    // display the overlay
    $("#dp-overlay").show();

    // show the labels
    $("#dropdown-inner label").css({
        visibility: "visible"
    });
    
    window.scrollTo(0, 0);
}

function hideSignInDropDown(){
    $("#dropdown").css({
        visibility: "hidden"
    });
            
    // hide the overlay
    $("#dp-overlay").hide();

    // don't show the labels
    $("#dropdown-inner label").css({
        visibility: "hidden"
    });
}

function doCancel(url){
    window.location = url.replace(/_/g, "-");
}

function doDone(url){
    window.location = url.replace(/_/g, "-");
}

function hasForm(data){
    return data.match(/method=\"post\"/);
}

function shouldProceed(data){
    return data.match(/proceed/);
}

function redirectToHome(time){
    setTimeout(function(){
        window.location.href = "/";
    }, time);
}

function reloadWindow(){
    setTimeout(function(){
        window.location.reload();
    }, 5000);
}

function requestLogin(){    
    $.fn.colorbox({
        href: '/profile-register',
        width:"890px", 
        height:"555px", 
        overlayClose: false,		
        "close":closeHtml
    });
}

function openPopup(url, width, height){    
    $.fn.colorbox({
        href: url,
        width: width, 
        height: height, 
        overlayClose: false,		
        "close": closeHtml
    });
}